/*
 * PhraseSerializer.cpp
 *
 *  Created on: Nov 21, 2012
 *      Author: m
 */

#include "PhraseSerializer.h"

namespace physical
{

PhraseSerializer::PhraseSerializer() :
        m_stringSerializer( new VariableStringSerializer )
{
}

PhraseSerializer::~PhraseSerializer()
{
}

size_t PhraseSerializer::serialize( const Phrase& element,
        byteStream block )
{
    byte* buffer = block;
    size_t usedBytes = 0;
    size_t totalBytesUsed = 0;

    usedBytes = m_stringSerializer->serialize( element.getText(), buffer );
    buffer += usedBytes;
    totalBytesUsed += usedBytes;

    usedBytes = m_stringSerializer->serialize( element.getAuthorSurname(),
            buffer );
    buffer += usedBytes;
    totalBytesUsed += usedBytes;

    usedBytes = m_stringSerializer->serialize( element.getAuthorName(),
            buffer );
    buffer += usedBytes;
    totalBytesUsed += usedBytes;

    return totalBytesUsed;
}

size_t PhraseSerializer::deserialize( const byteStream block,
        Phrase& element )
{
    const byte* buffer = block;
    size_t usedBytes = 0;
    size_t totalBytesUsed = 0;

    std::string text, authorSurname, authorName;
    usedBytes = m_stringSerializer->deserialize( buffer, text );
    buffer += usedBytes;
    totalBytesUsed += usedBytes;

    usedBytes = m_stringSerializer->deserialize( buffer, authorSurname );
    buffer += usedBytes;
    totalBytesUsed += usedBytes;

    usedBytes = m_stringSerializer->deserialize( buffer, authorName );
    buffer += usedBytes;
    totalBytesUsed += usedBytes;

    element = Phrase( text, authorSurname, authorName );

    return totalBytesUsed;
}

size_t PhraseSerializer::getSize( const Phrase& element )
{
    size_t totalBytesUsed = 0;

    totalBytesUsed = m_stringSerializer->getSize( element.getText() );
    totalBytesUsed = m_stringSerializer->getSize( element.getAuthorSurname() );
    totalBytesUsed = m_stringSerializer->getSize( element.getAuthorName() );

    return totalBytesUsed;
}

} /* namespace physical */
